//
//  MHVisitorLoginView.swift
//  MHSinaWeiBo
//
//  Created by a on 15/10/1.
//  Copyright © 2015年 icast. All rights reserved.
//

import UIKit

//定义协议
protocol VisitorLoginDelegate:NSObjectProtocol {
    //登陆
    func visitorLogin()
    
    //注册
    func visitorRegister()
}
class MHVisitorLoginView: UIView {
    
    //定义代理属性
    weak var delegate:VisitorLoginDelegate?
    
    
    //???
    override  init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUpUI()
    }
    
    func setUpUI(){
        
        //添加界面元素
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //添加约束
        setContraint()
    }
    
    func setUpInfo (isHome :Bool,messageText :String,icon :String){
        messageLabel.text = messageText
        homeIconView.image = UIImage(named: icon)
        
        //判断是不是首页，是首页显示小图标
        iconView.hidden = !isHome
        
        isHome ? startAnimate() : sendSubviewToBack(maskIconView)
    }
    
    func startAnimate (){
        //让小图标旋转
        //创建基本核心动画
        let basicAnim = CABasicAnimation(keyPath: "transform.rotation")
        basicAnim.toValue = M_PI*2
        basicAnim.repeatCount = MAXFLOAT
        basicAnim.duration = 3
        //??
        basicAnim.removedOnCompletion = false
        
        iconView.layer.addAnimation(basicAnim, forKey: "")
    }
    
    //弧形图标
    private lazy var iconView : UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconView
        }()
    
    //小房子
    private lazy var homeIconView :UIImageView = {
        let homeIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeIcon
        }()
    
    //文字标签
    private lazy var messageLabel :UILabel = {
        var label = UILabel ()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.sizeToFit()
        return label
        }()
    
    //注册按钮
    private lazy var registerButton :UIButton = {
        var registerBtn = UIButton()
        let oldImage = UIImage(named: "common_button_white_disable")
        
        let image = oldImage?.strectchWithImageName()

        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(image, forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        registerBtn.addTarget(self, action: "registerBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerBtn
        }()
    
    //登录按钮
    private lazy var loginButton : UIButton = {
        var loginBtn = UIButton()
        
        let oldImage = UIImage(named: "common_button_white_disable")
        
        let image = oldImage?.strectchWithImageName()
        
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(image , forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        loginBtn.addTarget(self, action: "loginBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
        }()
    
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        }()
    
     func registerBtnClick(sender:UIButton){
        //执行代理方法
        //delegate?.visitorRegister()
    }
    
     func loginBtnClick(sender:UIButton) {
        delegate?.visitorLogin()
    }
    
    
    
    
    //设置约束
    func setContraint() {
        //图标在屏幕居中
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //设置小房子和图标中心相同
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //设置文字y距图标60，x和图标中心对齐
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 60))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        //设置登录注册和文字左对齐，y距文字16，宽100，高35
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //设置登录按钮的 右边和文字右边对齐，y距文字16，宽100，高50
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-100-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
        
    }
}
